package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationappsdk.transferdetail.dto.AsyncRequest;
import com.epoch.app.otsapplicationappsdk.transferdetail.dto.InitRequest;
import com.epoch.app.otsapplicationappsdk.transferdetail.service.TransferDetailService;
import com.epoch.app.crunifiedinventory.api.allocateapplyorderread.service.AllocateApplyOrderReadService;
import com.epoch.app.crunifiedinventory.api.allocateapplyorderwrite.service.AllocateApplyOrderWriteService;
import com.epoch.app.crunifiedinventory.dto.request.CancelAllocateApplyOrderRequest;
import com.epoch.app.crunifiedinventory.dto.request.QueryAllocateApplyOrderRequest;
import com.epoch.app.crunifiedinventory.dto.response.AllocateApplyOrderDetailDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TransferDetailV2ServiceImpl implements TransferDetailService {

    @Resource
    AllocateApplyOrderReadService allocateApplyOrderReadService;

    @Resource
    AllocateApplyOrderWriteService allocateApplyOrderWriteService;

    private static Log log = Log.getLogger(TransferDetailV2ServiceImpl.class);


    @Override
    public Object init(InitRequest initRequest) {
        log.info("调拨预报单详情渲染 请求参数 {} " , JSON.toJSONString(initRequest));
        JSONObject result = new JSONObject();
        try {
            QueryAllocateApplyOrderRequest queryAllocateApplyOrderRequest = new QueryAllocateApplyOrderRequest();
            queryAllocateApplyOrderRequest.setCode(initRequest.getCode());
            Result<AllocateApplyOrderDetailDTO> callApiResult = allocateApplyOrderReadService.queryAllocateApplyOrderByCode(queryAllocateApplyOrderRequest);
            log.info("调拨预报单详情渲染 调拨预报单详情返回结果 {} " , JSON.toJSONString(callApiResult));
            result.put("success",true);
            result.put("result",callApiResult.getResult());
        } catch (Exception e) {
            log.info("调拨预报单详情渲染异常 {} " , e.getMessage());
            result.put("success",false);
            result.put("message","调拨预报单列表渲染异常");
        }

        return result;
    }

    @Override
    public Object async(AsyncRequest asyncRequest) {
        JSONObject result = new JSONObject();
        try {
            log.info("调拨预报单详情异步 请求参数 {} ", JSON.toJSONString(asyncRequest));
            JSONObject request = (JSONObject)JSONObject.toJSON(asyncRequest.getRequest());

            String code = null;
            JSONObject biModel = null;
            if (request != null) {
                if (request.get("fields") != null) {
                    JSONObject fieldsJson = request.getJSONObject("fields");
                    code = fieldsJson.getString("code");
                    biModel = fieldsJson.getJSONObject("bizModel");
                }
            }
            if (code != null) {
                CancelAllocateApplyOrderRequest cancelAllocateApplyOrderRequest = new CancelAllocateApplyOrderRequest();
                cancelAllocateApplyOrderRequest.setCode(code);
                Result<Boolean> cancelResult = allocateApplyOrderWriteService.cancelAllocateApplyOrder(cancelAllocateApplyOrderRequest);
                log.info("调拨预报单详情异步渲染 取消调拨返回结果 {} " , JSON.toJSONString(cancelResult));
                if (cancelResult != null) {
                    result.put("cancel",cancelResult.getResult());
                }

                QueryAllocateApplyOrderRequest queryAllocateApplyOrderRequest = new QueryAllocateApplyOrderRequest();
                queryAllocateApplyOrderRequest.setCode(code);
                Result<AllocateApplyOrderDetailDTO> callApiResult = allocateApplyOrderReadService.queryAllocateApplyOrderByCode(queryAllocateApplyOrderRequest);
                log.info("调拨预报单详情异步刷新数据 调拨预报单详情返回结果 {} " , JSON.toJSONString(callApiResult));
                result.put("result",callApiResult.getResult());
            }

            result.put("success",true);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("调拨预报单详情异步渲染异常 {}", e.getMessage());
            result.put("success",false);
            result.put("message",e.getMessage() != null ? e.getMessage() : "调拨预报单详情异步渲染异常");
        }

        return result;
    }

}
